package com.csh.szhm.adapter;

import java.util.ArrayList;
import java.util.List;

import com.csh.szhm.R;
import com.csh.szhm.SzhmApplication;
import com.csh.szhm.activity.SzhmBaiDuMapActivity;
import com.csh.szhm.activity.SzhmImageDetailActivity;
import com.csh.szhm.http.HttpUrls;
import com.csh.szhm.module.shop.SzhmImagePath;
import com.csh.szhm.module.shop.SzhmShopDetailBean;
import com.csh.szhm.utils.AppConfig;
import com.csh.szhm.utils.StringUtil;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SzhmShopDetailAdapter extends MyBaseAdapter<String> implements
		OnClickListener {

	private static final int TYPE_ONE = 0;
	private static final int TYPE_TWO = 1;

	public SzhmShopDetailAdapter(Context c) {
		super(c);
		// TODO Auto-generated constructor stub
	}

	private SzhmShopDetailBean mDetailData;

	public void addDetailData(SzhmShopDetailBean da) {
		mDetailData = da;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 1 + mData.size();
	}

	@Override
	public int getItemViewType(int position) {
		if (position == 0) {
			return TYPE_ONE;
		} else {
			return TYPE_TWO;
		}
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HolderViewTop holderViewTop = null;
		HolderView holderView = null;
		int type = getItemViewType(position);
		switch (type) {
		case TYPE_ONE:
			if (convertView == null) {
				holderViewTop = new HolderViewTop();
				convertView = mLayoutInflater.inflate(
						R.layout.szhm_shop_detail_item_top_layout, null);
				holderViewTop.name = (TextView) convertView
						.findViewById(R.id.detail_name_id);
				holderViewTop.img = (ImageView) convertView
						.findViewById(R.id.detail_img_id);
				holderViewTop.rat = (RatingBar) convertView
						.findViewById(R.id.ratingbar);
				holderViewTop.dh = (TextView) convertView
						.findViewById(R.id.detail_dh_txt_id);
				holderViewTop.dz = (TextView) convertView
						.findViewById(R.id.detail_dz_txt_id);
				holderViewTop.rj = (TextView) convertView
						.findViewById(R.id.detail_rj_id);
				holderViewTop.kw = (TextView) convertView
						.findViewById(R.id.detail_kw_id);
				holderViewTop.zk = (TextView) convertView
						.findViewById(R.id.detail_zk_id);
				holderViewTop.jj = (TextView) convertView
						.findViewById(R.id.detail_dh_jj_id);
				holderViewTop.map = (RelativeLayout) convertView
						.findViewById(R.id.detail_map_id);
				holderViewTop.phone = (RelativeLayout) convertView
						.findViewById(R.id.detail_phone_id);
				convertView.setTag(holderViewTop);
				holderViewTop.img.setOnClickListener(this);
				holderViewTop.map.setOnClickListener(this);
				holderViewTop.phone.setOnClickListener(this);
			} else {
				holderViewTop = (HolderViewTop) convertView.getTag();
			}
			if (mDetailData != null) {
				SzhmApplication.display(HttpUrls.PATH_ROOT
						+ mDetailData.getFirmInfo().get(0).getFirmImagePath(),
						holderViewTop.img);
				holderViewTop.name.setText(mDetailData.getFirmInfo().get(0)
						.getFirmTitle());
				holderViewTop.dh.setText(mDetailData.getFirmInfo().get(0)
						.getFirmPhone());
				holderViewTop.dz.setText(mDetailData.getFirmInfo().get(0)
						.getFirmAddr());
				holderViewTop.kw.setText("性价比:"
						+ mDetailData.getFirmInfo().get(0).getEvalTaste()
						+ "环境:"
						+ mDetailData.getFirmInfo().get(0).getEvalEnvir()
						+ "服务:"
						+ mDetailData.getFirmInfo().get(0).getEvalService());
				// holderViewTop.zk.setText("折扣："
				// + m + "");
				holderViewTop.rj.setText("营业时间："
						+ mDetailData.getFirmInfo().get(0).getTradeTmst());
				holderViewTop.jj.setText("简介："
						+ mDetailData.getFirmInfo().get(0).getFirmDesc());
				String total = mDetailData.getFirmInfo().get(0).getEvalTotal();
				if (!StringUtil.isEmptyString(total)) {
					int temp = Integer.valueOf(total);
					holderViewTop.rat.setRating(temp);
				} else {
					holderViewTop.rat.setRating(0);
				}
				SpannableStringBuilder builder = new SpannableStringBuilder(
						"折扣：" + mDetailData.getFirmInfo().get(0).getDiscount()
								+ " 折");
				ForegroundColorSpan redSpan = new ForegroundColorSpan(mContext
						.getResources().getColor(R.color.main_text_color));
				builder.setSpan(redSpan, 3, mDetailData.getFirmInfo().get(0)
						.getDiscount().length() + 3,
						Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				holderViewTop.zk.setText(builder);
			}

			break;
		case TYPE_TWO:
			if (convertView == null) {
				holderView = new HolderView();
				convertView = mLayoutInflater.inflate(
						R.layout.szhm_shop_back_item_layout, null);
				holderView.name = (TextView) convertView
						.findViewById(R.id.detail_pl_item_name_id);
				holderView.rat = (RatingBar) convertView
						.findViewById(R.id.ratingbar);
				holderView.rj = (TextView) convertView
						.findViewById(R.id.detail_pl_item_rj_id);
				holderView.content = (TextView) convertView
						.findViewById(R.id.detail_pl_item_content_id);

				holderView.imgRoot = (LinearLayout) convertView
						.findViewById(R.id.detail_pl_item_img_root_id);
				holderView.img1 = (ImageView) convertView
						.findViewById(R.id.detail_pl_item_img_1);
				holderView.img2 = (ImageView) convertView
						.findViewById(R.id.detail_pl_item_img_2);
				holderView.img3 = (ImageView) convertView
						.findViewById(R.id.detail_pl_item_img_3);
				convertView.setTag(holderView);
			} else {
				holderView = (HolderView) convertView.getTag();
			}
			if (position == 1) {
				holderView.imgRoot.setVisibility(View.VISIBLE);
			} else {
				holderView.imgRoot.setVisibility(View.GONE);
			}
			break;

		}
		return convertView;
	}

	private class HolderViewTop {
		TextView name;
		ImageView img;
		RatingBar rat;
		TextView rj;
		TextView kw;
		TextView dz;
		TextView dh;
		TextView zk;
		TextView jj;
		RelativeLayout map;
		RelativeLayout phone;
	}

	private class HolderView {
		TextView name;
		RatingBar rat;
		TextView rj;
		TextView content;
		LinearLayout imgRoot;
		ImageView img1;
		ImageView img2;
		ImageView img3;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.detail_img_id:
			if (mDetailData != null) {
				List<SzhmImagePath> ls = mDetailData.getFirmInfo().get(0)
						.getPictureList();
				if (ls != null) {
					ArrayList<String> temp = new ArrayList<String>();
					for (int i = 0; i < ls.size(); i++) {
						temp.add(ls.get(i).getImagePath());
					}
					Intent in = new Intent();
					in.putStringArrayListExtra("array", temp);
					startMyActivity(in, SzhmImageDetailActivity.class);
				}
			}
			break;
		case R.id.detail_map_id:
			if (mDetailData != null) {
				String lat = mDetailData.getFirmInfo().get(0).getLttude();
				String log = mDetailData.getFirmInfo().get(0).getLgtude();
				double lat1 = 0;
				double log1 = 0;
				// double lat1 = 28.61363;//维度
				// double log1 = 116.013685;//经度

				Intent in = new Intent();
				if (!StringUtil.isEmptyString(lat)
						&& !StringUtil.isEmptyString(log)) {
					lat1 = Double.parseDouble(lat);
					log1 = Double.parseDouble(log);
				}
				in.putExtra("lat", lat1);
				in.putExtra("lon", log1);
				startMyActivity(in, SzhmBaiDuMapActivity.class);
			}
			break;
		case R.id.detail_phone_id:
			if (mDetailData != null) {
				String phone = mDetailData.getFirmInfo().get(0).getFirmPhone();
				if (!StringUtil.isEmptyString(phone)) {
					AppConfig.callPhone(phone, mContext);
				}
			}
			break;
		}

	}
}
